<?php
require_once 'config.php'; 
require_once 'startup.php'; 

// Check for user login
if(!$user->isLogged()){ 
    header('Location: ' . $config['url_admin'] . 'login.php');    
    exit();
}

// Render index page
$page_title = 'Havhokeren - Administration';
$title = 'Edit subscriber';
$filter = false;

$logged = $user->getUserName();
$save = '#';
$cancel = $config['url_admin'] . 'index.php';
$logout = $config['url_admin'] . 'logout.php';     
$edit_user = $config['url_admin'] . 'changepassword.php';
$newsletter = $config['url_admin'] . 'newsletter.php';  

$text_save = 'Save';
$text_cancel = 'Cancel';
$text_logout = 'Logout';
$text_edit_user = 'Change password';
$text_subscriber = 'Subscribers';
$text_newsletter = 'Newsletter';
$text_email = 'Email address';
$text_date = 'Date register';
$text_goback = 'Click here to go back to subscribers list!';
$item_saved = false;
 
// Get data to insert, edit or delete
$email = isset($request->post['email']) ? $request->post['email'] : '';
$date = isset($request->post['date']) ? $request->post['date'] : $date = date('d-m-Y');   
$action = isset($request->post['action']) ? $request->post['action'] : 'insert';   
    
if($request->server['REQUEST_METHOD'] == 'POST' && isset($request->post['method'])){      
    switch($request->post['method']){
    case 'delete':
        if(isset($request->post['selected'])){
            foreach($request->post['selected'] as $email){
                $db->query("DELETE FROM " . DB_PREFIX . "newsletter WHERE email = '" . $db->escape($email) . "'");
            }    
            $item_saved = true;
            unset($error);
            $success = 'Item deleted!';                 
        }
        else{
            $item_saved = true;
            $error = 'Error while processing.';
            unset($success);   
        }
        
        break;
    case 'edit':
    case 'insert':     
        $email = isset($request->post['email']) ? $request->post['email'] : '';
        $action = $request->post['method'];
        
        $row = $db->query("SELECT * FROM " . DB_PREFIX . "newsletter WHERE email = '" . $db->escape($email) . "'");        
        if($row->num_rows){            
            $email = $row->row['email'];
            $date = strftime('%d-%m-%Y', strtotime($row->row['date_added']));
        }
        else{
            $email = '';
            $date = date('d-m-Y');
        }
        
        break;
    }    
}    

if($request->server['REQUEST_METHOD'] == 'POST' && !isset($request->post['method'])){    
    $pattern = '/^[A-Z0-9._%-+]+@[A-Z0-9][A-Z0-9.-]{0,61}[A-Z0-9]\.[A-Z]{2,6}$/i';         
    if(isset($request->post['email']) && preg_match($pattern, $request->post['email'])){
        if($action == 'insert'){
            $row = $db->query("SELECT * FROM " . DB_PREFIX . "newsletter WHERE email = '" . $db->escape($request->post['email']) . "'");        
            if(!$row->num_rows){                     
                $db->query("INSERT INTO " . DB_PREFIX . "newsletter SET email = '" . $db->escape($request->post['email']) . "', date_added = NOW()");
                
                $item_saved = true;
                unset($error);
                $success = 'Item saved!';                 
                //header('Location: ' . $config['url_admin'] . 'index.php');   
            }
            else{
                $error = 'This email has been registered.';
                unset($success);    
            }
        }
        elseif($action == 'edit'){
            
        }
    }   
    else{
        $error = 'Please enter an valid email address.';
        unset($success);
    } 
}

// Render page
include_once 'header.php';
?>

<script type="text/javascript"><!--
    jQuery(document).ready(function(){
        jQuery('#save_subscriber').click(function(even){
            even.preventDefault(); 

            jQuery('#edit_subscriber').submit();
        });
    });         
//-->
</script>

<body>
    <div id="header">
        <div class="div1"><img style="cursor: pointer;" src="<?php echo $config['url_admin']; ?>image/logo.png" title="<?php echo $title; ?>" onclick="location = '<?php echo $config['url_admin']; ?>'" /></div>
        <?php if ($logged) { ?>
            <div class="div2"><img src="<?php echo $config['url_admin']; ?>image/lock.png" alt="" style="position: relative; top: 3px;" />You are logged in as: &nbsp;<?php echo $logged; ?></div>
        <?php } ?>
    </div>
    
    <?php if ($logged) { ?>
        <div id="menu">
            <ul class="nav left" style="display: block;">
                <li id="subscriber" class="selected"><a class="top" href="javascript:void(0);"><?php echo $text_subscriber; ?></a></li>
                <li id="newsletter"><a class="top" href="<?php echo $newsletter; ?>"><?php echo $text_newsletter; ?></a></li>                
            </ul>
            <ul class="nav right">                
                <li id="edit_user"><a class="top" href="<?php echo $edit_user; ?>"><?php echo $text_edit_user; ?></a></li>
                <li id="logout"><a class="top" href="<?php echo $logout; ?>"><?php echo $text_logout; ?></a></li>
            </ul>
        </div>
    <?php } ?>
    
    <div id="content">
        <div class="breadcrumb"></div>
        
        <?php if(isset($error)) { ?> 
            <div class="warning"><?php echo $error; ?></div>
        <?php } ?>
        <?php if(isset($success)){ ?>
            <div class="success"><?php echo $success;?></div>
        <?php } ?>
    
        <div class="box">
            <div class="left"></div>
            <div class="right"></div>
            <div class="heading">
                <h1 style="background-image: url('<?php echo $config['url_admin']; ?>image/mail.png');"><?php echo $title; ?></h1>
                <div class="buttons">
                     <?php if(!$item_saved){ ?>
                        <a id="save_subscriber" href="<?php echo $save; ?>" class="button"><span><?php echo $text_save; ?></span></a>
                        <a onclick="location = '<?php echo $cancel; ?>'" class="button"><span><?php echo $text_cancel; ?></span></a>
                     <?php } ?>
                </div>
            </div>
            
            <div class="content">      
                <form id="edit_subscriber" action="<?php echo $config['url_admin'] . basename(__FILE__); ?>" method="post" enctype="multipart/form-data">
                    <table class="list">                         
                        <tbody>
                            <?php if(!$item_saved){ ?>
                                <tr>
                                    <td class="right" width="15%"><?php echo $text_email; ?><span class="required">*</span></td>
                                    <td class="left" width="35%"><input type="text" id="email" value="<?php echo $email; ?>" name="email" style="width:300px;"></td>
                                    <td class="right" width="15%"><?php echo $text_date; ?></td>
                                    <td class="left" width="35%"><input type="text" disabled="disabled" value="<?php echo $date; ?>" name="date" style="background: #C0C0C0;"></td>
                                </tr> 
                            <?php }else{ ?>
                                <tr>
                                    <td class="center" colspan="3"><a href="<?php echo $cancel; ?>"><?php echo $text_goback; ?></a></td>
                                </tr>
                            <?php } ?>                           
                        </tbody>
                    </table>
                    <input type="hidden" id="action" name="action" value="<?php echo $action; ?>" />   
                </form>                    
            </div>
        </div>
    </div>
    
    <?php include_once 'footer.php'; ?>
</body>
</html>
